﻿Imports Entities
Public Class ClienteComercialDAL
    Public Shared Function List() As List(Of clientescomerciales)
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.clientescomerciales
                          Where rec.ACTIVO
                          Order By rec.RAZONSOCIAL Ascending
                          Select rec

                Return query.ToList
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function Create(ByVal zObject As clientescomerciales) As clientescomerciales
        Try
            Using bd As New gas_gasdesuvecinoEntities
                bd.clientescomerciales.Add(zObject)
                bd.SaveChanges()
                Return zObject
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function Update(ByVal zObject As clientescomerciales) As clientescomerciales
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = bd.clientescomerciales.Find(zObject.CLIENTE)

                query.RUT = zObject.RUT
                query.RAZONSOCIAL = zObject.RAZONSOCIAL
                query.DIRECCION = zObject.DIRECCION
                query.GIRO = zObject.GIRO
                query.COMUNA = zObject.COMUNA
                query.CIUDAD = zObject.CIUDAD
                query.NOMBRECONTACTO = zObject.NOMBRECONTACTO
                query.FONOCONTACTO = zObject.FONOCONTACTO
               
                bd.SaveChanges()
                Return query
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function Delete(ByVal zId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim del = bd.clientescomerciales.Find(zId)

                del.ACTIVO = False
                bd.SaveChanges()
                Return True
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function
    Public Shared Function Exist(ByVal zId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.clientescomerciales
                            Where rec.CLIENTE = zId
                            Select rec).Count
                If query > 0 Then
                    Return True
                Else
                    Return False
                End If
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        End Try
    End Function

    Public Shared Function SearchByRut(ByVal zRut As String) As clientescomerciales
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.clientescomerciales
                            Where rec.RUT = zRut And rec.ACTIVO
                            Select rec).Single
                Return query
            End Using
        Catch ex As Exception
            Debug.Print("ProveedorDAL.SearchByRut " + ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function SearchById(ByVal zId As Long) As clientescomerciales
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.clientescomerciales
                          Where rec.ACTIVO
                          Select rec

                Return query.SingleOrDefault
            End Using
        Catch ex As Exception
            Debug.Print("ProveedorDAL.SearchByRut " + ex.Message)
            Return Nothing
        End Try
    End Function
End Class
